import java.util.ArrayList;
import java.util.Collections;



public class CartOfItems {
	
	ArrayList<DVD> dvds;
	
	public CartOfItems()
	{
		dvds = new ArrayList<DVD>();
	}
	
	public CartOfItems(CartOfItems c)
	{
		dvds = new ArrayList<DVD>();
		if (c != null)
		{
			for (int i = 0; i < c.getAllDvds().size(); i++)
			{
				DVD dvdToCopy = new DVD(c.getAllDvds().get(i));
				dvds.add(dvdToCopy);
			}
		}
	}
	
	public void addDVD(DVD d)
	{
		boolean foundDvd = false;
		for (int i = 0; i < dvds.size(); i++)
			if (dvds.get(i).getID() == d.getID())
			{
				dvds.get(i).setQuantity(dvds.get(i).getQuantity() + d.getQuantity());
				foundDvd = true;
				break;
			}
		
		if (!foundDvd)
		{
			dvds.add(d);
			Collections.sort(dvds);
		}
	}
	
	public void removeDvdWithId(int id)
	{
		for (int i = 0; i < dvds.size(); i++)
			if (dvds.get(i).getID() == id)
			{
				dvds.remove(i);
				break;
			}
	}
	
	public void setExistingDVD(DVD d)
	{
		
		for (int i = 0; i < dvds.size(); i++)
				if (dvds.get(i).getID() == d.getID())
					dvds.get(i).setQuantity(d.getQuantity());
	}
	
	public DVD getDvdByPosition(int pos)
	{
		return dvds.get(pos);
	}
	
	public DVD getDvdById(int id)
	{
		for (int i = 0; i < dvds.size(); i++)
			if (dvds.get(i).getID() == id)
				return dvds.get(i);
		
		return null;
	}
	
	public ArrayList<DVD> getZeroQuantityDvds()
	{
		ArrayList<DVD> zeroDvds = new ArrayList<DVD>();
		
		for (int i = 0; i < dvds.size(); i++)
			if (dvds.get(i).getQuantity() == 0)
				zeroDvds.add(dvds.get(i));
		
		return zeroDvds;
	}
	
	public ArrayList<DVD> getAllDvds()
	{
		return dvds;
	}
	
	public int totalNumberOfDVDs()
	{
		int count = 0;
		for (int i = 0; i < dvds.size(); i++)
		{
			count += dvds.get(i).getQuantity();
		}
		
		return count;
	}
	
	public String toString()
	{
		return dvds.toString();
	}
}
